package com.cloudwise.controller;

import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.Role;
import com.cloudwise.entity.RoleVo;
import com.cloudwise.service.RoleService;
import com.cloudwise.util.MyConstants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2020-12-17 10:17:53
 */
@Controller
@RequestMapping("role")
public class RoleController  extends  BaseController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Role selectOne(Integer id) {
        return this.roleService.queryById(id);
    }


    /**
     * create by: Teacher陈
     * description:请求ShowDept，跳转到显示权限树的页面
     * create time: 2020/7/17 16:28
     *
     * @Param:
     * @return java.lang.String
     */
    @RequestMapping("/toShowRole")
    public String toShowRole(){
        return "role/showRole";
    }

    /**
     * @create by: Teacher陈
     * @description: 查询所有的角色信息
     * @create time: 2020/7/19 15:52
     * @return LayUiTable
     */
    @RequestMapping("/findAllRoles")
    @ResponseBody
    public ReturnBean findAllRoles(Role role){
        List<Role> roleList = roleService.queryAll(role);
        return  returnSuccess(roleList);
    }

    /**
     * @create by: Teacher陈
     * @description: 添加角色，并指定角色对应的菜单
     * @create time: 2020/7/19 18:36
     * @param roleVo
     * @return Result
     */
    @RequestMapping("/saveRole")
    @ResponseBody
    public ReturnBean saveRole(@RequestBody RoleVo roleVo){

        boolean save = roleService.saveRoleAndMenus(roleVo);
        if (save) {
            return returnSuccess(null);
        } else {
            return returnFail(null);
        }
    }
    @RequestMapping("/updateRole")
    @ResponseBody
    public ReturnBean updateRole(@RequestBody RoleVo roleVo){
        setUpdate(roleVo);
        boolean update = roleService.updateRoleAndMenus(roleVo);
        if (update) {
            return returnSuccess(null);
        } else {
            return returnFail(null);
        }
    }
    /**
     * @create by: Teacher陈
     * @description: 删除角色，并且把相关的用户中间表和菜单中间表也同时删除
     * @create time: 2020/7/19 21:10
     * @param role
     * @return Result
     */
    @RequestMapping("/deleteRole")
    @ResponseBody
    public  ReturnBean deleteRole(Role role){
        boolean delete = roleService.deleteRoleAndMenus(role);
        if (delete) {
            return returnSuccess(null);
        } else {
            return returnFail(null);
        }
    }
    /**
     * @create by: Teacher陈
     * @description: 返回角色对应的菜单列表
     * @create time: 2020/7/19 22:04
     * @param roleId
     * @return Result
     */
    @RequestMapping("/findMenuIdsByRoleId")
    @ResponseBody
    public ReturnBean findMenuIdsByRoleId(Integer roleId){
        List<Integer> menuIds=roleService.findMenuIdsByRoleId(roleId);
        return returnSuccess(menuIds);
    }
}